package co.inventario.modelo.fachada;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;


import co.inventario.entidad.KardexDTO;
import co.inventario.modelo.negocio.KardexNegocio;
import co.inventario.transversal.DAOFactorySQLServer;

public class KardexFachada {
	
	public void crear(KardexDTO kardexDTO, Connection connection) throws Exception, SQLException {
		DAOFactorySQLServer daoFactorySQLServer = new DAOFactorySQLServer();
		KardexNegocio kardexNegocio = new KardexNegocio();
		try {
			kardexNegocio.crear(kardexDTO, daoFactorySQLServer.obtenerConexion());
			daoFactorySQLServer.confirmarTransaccion();
		} catch (SQLException exception) {
			daoFactorySQLServer.cancelarTransaccion();
			throw new Exception("Se ha presentado un problema intentando crear el kardex.");
		} catch (Exception exception) {
			daoFactorySQLServer.cancelarTransaccion();
			throw exception;
		} finally {
			daoFactorySQLServer.cerrarConexion();
		}
	}

	public void actualizar(KardexDTO kardexDTO, Connection connection) throws Exception, SQLException {
		DAOFactorySQLServer daoFactorySQLServer = new DAOFactorySQLServer();
		KardexNegocio kardexNegocio = new KardexNegocio();
		try {
			kardexNegocio.actualizar(kardexDTO, daoFactorySQLServer.obtenerConexion());
			daoFactorySQLServer.confirmarTransaccion();
		} catch (SQLException exception) {
			daoFactorySQLServer.cancelarTransaccion();
			throw new Exception("Se ha presentado intentando actualizar el kardex.");
		} catch (Exception exception) {
			throw exception;
		} finally {
			daoFactorySQLServer.cerrarConexion();
		}
	}

	public ArrayList<KardexDTO> consultarTodo(Connection connection) throws Exception, SQLException {
		DAOFactorySQLServer daoFactorySQLServer = new DAOFactorySQLServer();
		KardexNegocio kardexNegocio = new KardexNegocio();
		ArrayList<KardexDTO> kardexDTOLista = null;
		try {
			kardexDTOLista = kardexNegocio.consultarTodo(daoFactorySQLServer.obtenerConexion());
		} catch (SQLException exception) {
			daoFactorySQLServer.cancelarTransaccion();
			throw new Exception("Se ha presentado intentando consultar los kardex.");
		} catch (Exception exception) {
			throw exception;
		} finally {
			daoFactorySQLServer.cerrarConexion();
		}
		return kardexDTOLista;
	}
	
}
